package com.ssy.lingxi.pay.controller.pc.assetAccount;

import com.ssy.lingxi.common.constant.member.MemberLevelTypeEnum;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.pay.api.dto.request.assetAccount.*;
import com.ssy.lingxi.pay.api.dto.response.assetAccount.AccountCashOutRecordResponse;
import com.ssy.lingxi.pay.api.dto.response.assetAccount.AccountTradeRecordResponse;
import com.ssy.lingxi.pay.api.dto.response.assetAccount.MemberAssetAccountResponse;
import com.ssy.lingxi.pay.common.TradeStatusEnum;
import com.ssy.lingxi.pay.entity.assetAccount.AccountCashOutRecord;
import com.ssy.lingxi.pay.entity.assetAccount.AccountStatusRecord;
import com.ssy.lingxi.pay.entity.assetAccount.AccountTradeRecord;
import com.ssy.lingxi.pay.entity.assetAccount.MemberAssetAccount;
import com.ssy.lingxi.pay.model.assetAccount.CashOutQueryQuery;
import com.ssy.lingxi.pay.model.assetAccount.MemberAssetAccountQuery;
import com.ssy.lingxi.pay.service.assetAccount.IAccountCashOutRecordService;
import com.ssy.lingxi.pay.service.assetAccount.IAccountStatusService;
import com.ssy.lingxi.pay.service.assetAccount.IAccountTradeService;
import com.ssy.lingxi.pay.service.assetAccount.IMemberAssetAccountService;
import org.modelmapper.TypeToken;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @menu: 会员资金账户管理
 * 会员资金账户
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/6/28
 */
@RestController
@RequestMapping("/pay/memberAssetAccount")
public class MemberAssetAccountController extends BaseController {

    @Resource
    private IMemberAssetAccountService memberAssetAccountService;

    @Resource
    private IAccountTradeService accountTradeService;

    @Resource
    private IAccountStatusService accountStatusService;

    @Resource
    private IAccountCashOutRecordService accountCashOutRecordService;

    /**
     * 查询会员资金账户列表
     * @param pageVO 分页实体
     * @param memberAssetAccountQueryRequest 参数
     */
    @RequestMapping(value = "/getMemberAssetAccountList", method = RequestMethod.GET)
    public Wrapper<PageData<MemberAssetAccountResponse>> getMemberAssetAccountList(PageVO pageVO, MemberAssetAccountQueryRequest memberAssetAccountQueryRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Long memberId = sysUser.getMemberId();
        Long memberRoleId = sysUser.getMemberRoleId();
        MemberAssetAccountQuery memberAssetAccountMiddle = this.modelMapper.map(memberAssetAccountQueryRequest, MemberAssetAccountQuery.class);
        memberAssetAccountMiddle.setParentMemberId(memberId);
        memberAssetAccountMiddle.setParentMemberRoleId(memberRoleId);
        List<Integer> memberLevelTypeList = new ArrayList<>();
        memberLevelTypeList.add(MemberLevelTypeEnum.MERCHANT.getCode());
        memberLevelTypeList.add(MemberLevelTypeEnum.CHANNEL.getCode());
        memberAssetAccountMiddle.setMemberLevelTypeList(memberLevelTypeList);

        Page<MemberAssetAccount> result = memberAssetAccountService.getMemberAssetAccountList(pageVO, memberAssetAccountMiddle);
        List<MemberAssetAccountResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<MemberAssetAccountResponse>>(){}.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 会员资金账户详情
     * @param id 会员资金账户id
     */
    @RequestMapping(value = "/getMemberAssetAccount", method = RequestMethod.GET)
    public Wrapper<MemberAssetAccountResponse> getMemberAssetAccount(@RequestParam Long id) {
        MemberAssetAccount memberAssetAccount = memberAssetAccountService.getMemberAssetAccount(id);
        if(memberAssetAccount != null){
            return Wrapper.success(this.modelMapper.map(memberAssetAccount, MemberAssetAccountResponse.class));
        }else{
            return Wrapper.success(null);
        }
    }

    /**
     * 交易记录
     * @param memberAssetAccountId 会员资金账户id
     */
    @RequestMapping(value = "/getAccountTradeRecord", method = RequestMethod.GET)
    public Wrapper<PageData<AccountTradeRecord>> getAccountTradeRecord(PageVO pageVO, @RequestParam("memberAssetAccountId") Long memberAssetAccountId) {
        Page<AccountTradeRecord> result = accountTradeService.getAccountTradeRecord(pageVO, memberAssetAccountId);
        return Wrapper.success(new PageData<>(result.getTotalElements(), result.getContent()));
    }

    /**
     * 状态记录
     * @param memberAssetAccountId 会员资金账户id
     */
    @RequestMapping(value = "/getAccountCheckRecord", method = RequestMethod.GET)
    public Wrapper<List<AccountStatusRecord>> getAccountCheckRecord(@RequestParam("memberAssetAccountId") Long memberAssetAccountId) {
        return Wrapper.success(accountStatusService.getAccountStatusRecord(memberAssetAccountId));
    }

    /**
     * 冻结/解冻会员资金账户
     * @param simpleStatusRequest 参数
     */
    @RequestMapping(value = "/updateMemberAssetAccountEnable", method = RequestMethod.POST)
    public Wrapper<String> updateMemberAssetAccountEnable(@RequestBody SimpleStatusRequest simpleStatusRequest) {
        return Wrapper.success(memberAssetAccountService.updateMemberAssetAccountEnable(simpleStatusRequest.getId(), simpleStatusRequest.getStatus(), simpleStatusRequest.getRemark()));
    }

    /**
     * 查询待审核提现列表
     * @param cashOutQueryRequest 参数
     */
    @RequestMapping(value = "/getCheckCashOutList", method = RequestMethod.GET)
    public Wrapper<PageData<AccountTradeRecordResponse>> getCheckCashOutList(PageVO pageVO, CashOutQueryRequest cashOutQueryRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        //构建查询条件
        CashOutQueryQuery cashOutQueryQuery = new CashOutQueryQuery();
        cashOutQueryQuery.setParentMemberId(sysUser.getMemberId());
        cashOutQueryQuery.setParentMemberRoleId(sysUser.getMemberRoleId());
        cashOutQueryQuery.setMemberName(cashOutQueryRequest.getMemberName());
        cashOutQueryQuery.setStartTime(cashOutQueryRequest.getStartTime());
        cashOutQueryQuery.setEndTime(cashOutQueryRequest.getEndTime());

        //会员等级类型
        List<Integer> memberLevelTypeList = new ArrayList<>();
        memberLevelTypeList.add(MemberLevelTypeEnum.MERCHANT.getCode());
        memberLevelTypeList.add(MemberLevelTypeEnum.CHANNEL.getCode());
        cashOutQueryQuery.setMemberLevelTypeList(memberLevelTypeList);

        //状态
        List<Integer> statusList = new ArrayList<>();
        Integer status = cashOutQueryRequest.getStatus();
        if(status != null && status > 0){
            statusList.add(status);
        }else{
            statusList.add(TradeStatusEnum.ApplyCashOut.getCode());
            statusList.add(TradeStatusEnum.CheckPass.getCode());
            statusList.add(TradeStatusEnum.CheckNotPass.getCode());
        }
        cashOutQueryQuery.setStatusList(statusList);

        Page<AccountTradeRecord> result = accountTradeService.getCheckCashOutList(pageVO, cashOutQueryQuery);
        List<AccountTradeRecordResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<AccountTradeRecordResponse>>(){}.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 批量审核通过--提现
     * @param simpleIdListRequest 参数
     */
    @RequestMapping(value = "/batchCheck", method = RequestMethod.POST)
    public Wrapper<String> batchCheck(@RequestBody SimpleIdListRequest simpleIdListRequest) {
        return Wrapper.success(accountTradeService.batchCheck(simpleIdListRequest));
    }

    /**
     * 审核提现
     * @param checkRequest 参数
     */
    @RequestMapping(value = "/check", method = RequestMethod.POST)
    public Wrapper<String> check(@RequestBody CheckRequest checkRequest) {
        return Wrapper.success(accountTradeService.check(checkRequest));
    }

    /**
     * 查询待支付提现列表
     * @param cashOutQueryRequest 参数
     */
    @RequestMapping(value = "/getPayCashOutList", method = RequestMethod.GET)
    public Wrapper<PageData<AccountTradeRecordResponse>> getPayCashOutList(PageVO pageVO, CashOutQueryRequest cashOutQueryRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        //构建查询条件
        CashOutQueryQuery cashOutQueryQuery = new CashOutQueryQuery();
        cashOutQueryQuery.setParentMemberId(sysUser.getMemberId());
        cashOutQueryQuery.setParentMemberRoleId(sysUser.getMemberRoleId());
        cashOutQueryQuery.setMemberName(cashOutQueryRequest.getMemberName());
        cashOutQueryQuery.setStartTime(cashOutQueryRequest.getStartTime());
        cashOutQueryQuery.setEndTime(cashOutQueryRequest.getEndTime());

        //会员等级类型
        List<Integer> memberLevelTypeList = new ArrayList<>();
        memberLevelTypeList.add(MemberLevelTypeEnum.MERCHANT.getCode());
        memberLevelTypeList.add(MemberLevelTypeEnum.CHANNEL.getCode());
        cashOutQueryQuery.setMemberLevelTypeList(memberLevelTypeList);

        //状态
        List<Integer> statusList = new ArrayList<>();
        Integer status = cashOutQueryRequest.getStatus();
        if(status != null && status > 0){
            statusList.add(status);
        }else{
            statusList.add(TradeStatusEnum.CheckPass.getCode());
            statusList.add(TradeStatusEnum.CashOutSuccess.getCode());
            statusList.add(TradeStatusEnum.CashOutFail.getCode());
        }
        cashOutQueryQuery.setStatusList(statusList);

        Page<AccountTradeRecord> result = accountTradeService.getCheckCashOutList(pageVO, cashOutQueryQuery);
        List<AccountTradeRecordResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<AccountTradeRecordResponse>>(){}.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 支付提现
     * @param payRequest 参数
     */
    @RequestMapping(value = "/payCashOut", method = RequestMethod.POST)
    public Wrapper<String> payCashOut(@RequestBody PayRequest payRequest) {
        return Wrapper.success(memberAssetAccountService.payCashOut(payRequest));
    }

    /**
     * 查询提现处理记录列表
     * @param tradeCode 交易流水号
     */
    @RequestMapping(value = "/getCashOutRecordList", method = RequestMethod.GET)
    public Wrapper<PageData<AccountCashOutRecordResponse>> getCashOutRecordList(PageVO pageVO, @RequestParam("tradeCode") String tradeCode) {
        Page<AccountCashOutRecord> result = accountCashOutRecordService.getCheckCashOutList(pageVO, tradeCode);
        List<AccountCashOutRecordResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<AccountCashOutRecordResponse>>(){}.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }
}
